<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\Shop;

use App\Exceptions\ApiException;
use App\Models\Base;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 * Class GoodsSku
 *
 * @property int $id
 * @property string $sku_number
 * @property int $goods_id
 * @property array $attribute_list
 * @property float $sku_price
 * @property float $original_price
 * @property int $stock
 * @property bool $is_default
 * @property Carbon $created_at
 * @property Carbon $updated_at
 *
 * @package App\Models
 */
class GoodsSku extends Base
{
	protected $table = 'goods_sku';

	protected $casts = [
		'goods_id' => 'int',
		'attribute_list' => 'json',
		'sku_price' => 'float',
		'original_price' => 'float',
		'stock' => 'int',
		'is_default' => 'bool'
	];

	protected $fillable = [
		'sku_number',
		'goods_id',
		'attribute_list',
		'sku_price',
		'original_price',
		'stock',
		'is_default'
	];

    /**
     * adminSaveSku 后台提交的 sku 的验证
     * @time:2020/4/7-17:53
     *
     * @author:dongdong
     * @queryParam example_id required The id of the location.
     * @bodyParam example_id required The id of the location.
     * @response {
     * "data": {
     * },
     * "status_code": 200,
     * "message" : "success"
     * }
     */
	public function adminSaveSkuValidate(array $skuArr) {


            $validator = Validator::make($skuArr,[
                'type' => 'required',
                'attrs' => 'required',
                'sku' => 'required',
            ]);
            if ($validator->fails()) {
                $message = $validator->errors()->first();
                throw new Exception("数据验证错误--".$message);
            }
            return true;

    }

    /**
     * saveSku
     * @time:2020/4/7-10:11
     * 保存sku
     * @author:dongdong
     * @queryParam example_id required The id of the location.
     * @bodyParam example_id required The id of the location.
     * @response {
     * "data": {
     * },
     * "status_code": 200,
     * "message" : "success"
     * }
     */
    public function saveSku(string $jsonSku, int $goods_id)
    {
        $skuArr = json_decode($jsonSku,true);
        if (!is_array($skuArr)) {
            throw new Exception("sku数据验格式错误--不是一个数组");
        }

        $boolean = $this->adminSaveSkuValidate($skuArr);
        if (!$boolean) {
            throw new Exception("sku数据验失败");
        }

        DB::beginTransaction();
        $sku = $skuArr['sku'];
        switch ($skuArr['type']) {
            case 'single':
                GoodsSku::query()->where('goods_id', $goods_id)->delete();
                GoodsSku::create([
                    'sku_number' => makeNumber('sku_') ,
                    'goods_id' => $goods_id,
                    'sku_price' => $sku['price']??0,
                    'original_price' =>  $skuArr['price_old']??0,
                    'stock' => $skuArr['stock'],
                    'is_default' => true,
                ]);
                break;
            case 'many':
                dd($skuArr);
                break;
        }


    }
}
